package okhttp3.guide;

import java.io.IOException;
import java.net.InetAddress;
import java.util.logging.Logger;

import javax.net.ssl.HttpsURLConnection;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class GetExample {
	  private static final Logger logger = Logger.getLogger(GetExample.class.getName());
	 static OkHttpClient client1 = new OkHttpClient.Builder().build();
	  OkHttpClient client = new OkHttpClient.Builder()
//		  .addInterceptor(new Interceptor() {
//
//			@Override
//			public Response intercept(Chain chain) throws IOException {
//				Request request = chain.request();
//				Request rq = new Request.Builder()
//						.url("http://www.baidu.com")
//						.headers(request.headers())
//						.build();
//				return client1.newCall(rq).execute();
//			}
//			  
//		  })
		  .build();
	 
  String run(String url) throws IOException {
	  
	  Request request = new Request.Builder()	
        .url(url)
        .build();
	 client.newCall(request).enqueue(new Callback() {
		
		@Override
		public void onResponse(Call call, Response response) throws IOException {
			 System.out.println("1 call thread name "+Thread.currentThread().getName()+"  call  "+response.body().string());
		}
		
		@Override
		public void onFailure(Call call, IOException e) {
			
		}
	});
	 client.newCall(request).enqueue(new Callback() {
			
			@Override
			public void onResponse(Call call, Response response) throws IOException {
				 System.out.println("2 call thread name "+Thread.currentThread().getName()+"  call  "+response.body().string());
			}
			
			@Override
			public void onFailure(Call call, IOException e) {
				e.printStackTrace();
			}
		});
//    try (Response response = client.newCall(request).execute()) {
//      return response.body().string();
//    }
    return "1";
  }

  public static void main(String[] args) throws IOException {
    GetExample example = new GetExample();
    InetAddress[] addr=InetAddress.getAllByName("www.baidu.com");
    for(int i=0;i<addr.length;i++) {
      System.out.println(addr[i]);
    }
   // String response = example.run("https://raw.github.com/square/okhttp/master/README.md");//https://www.baidu.com/
   //String response = example.run("http://67zixue.com/home/article/detail/id/32.html");
    String response = example.run("http://www.baidu.com");
    System.out.println("response start ============>");
     
    System.out.println(response);
  }
  
//  private static class LoggingInterceptor implements Interceptor {
//	    @Override public Response intercept(Chain chain) throws IOException {
//	      long t1 = System.nanoTime();
//	      Request request = chain.request();
//	      System.out.println("--------LoggingInterceptor");
//	      logger.info(String.format("Sending request %s on %s%n%s",
//	          request.url(), chain.connection(), request.headers()));
//	      Response response = chain.proceed(request);
//
//	      long t2 = System.nanoTime();
//	      logger.info(String.format("Received response for %s in %.1fms%n%s",
//	          request.url(), (t2 - t1) / 1e6d, response.headers()));
//	      return response;
//	    }
//	  }
}
